package com.example.damaimaven.controller;

import com.example.damaimaven.entity.Orders;
import com.example.damaimaven.entity.Result;
import com.example.damaimaven.entity.User;
import com.example.damaimaven.service.BizException;
import com.example.damaimaven.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzh
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    IOrdersService ordersService;

    @RequestMapping("commit")
    public Result commit(@RequestBody Orders orders,
                         @SessionAttribute(required = false) User myinfo){
        try {
            if(myinfo == null){
                throw  new BizException("请先登录系统!");
            }
            orders.setUid(myinfo.getUid());
            ordersService.commit(orders);
            return  Result.ok("订单提交成功!");
        } catch (BizException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常，请联系客服!");
        }
    }

}
